import Vue from 'vue'
import axios from "axios";
import nprogress from 'nprogress'
import '../../node_modules/_nprogress@0.2.0@nprogress/nprogress.css'
import store from '../store/store.js'
// 第一个
let ProductionBesaUrl = null;
if (process.env.NODE_ENV === 'production') {
    ProductionBesaUrl = 'http://39.98.123.211/api'
}
const axios_1 = axios.create({
    baseURL: ProductionBesaUrl? ProductionBesaUrl : 'http://localhost:8080/api',
    timeout: 5000
});                                     
axios_1.interceptors.request.use((config) => {
    let uuid_token = store.state.shopcart.uuid_token
    let token = store.state.login.token
    if (uuid_token) {
        config.headers.userTempId = uuid_token
    }
    if (token) {
        config.headers.token = token
    }
    nprogress.start()
    return config
});
axios_1.interceptors.response.use((data) => {
    nprogress.done()
    return data.data
}, () => {
    nprogress.done()
});

// 第二个 用于mockjs
const axios_2 = axios.create({
    timeout: 5000
});
axios_2.interceptors.request.use((config) => {
    nprogress.start()
    return config
});
axios_2.interceptors.response.use((data) => {
    nprogress.done()
    return data.data
}, (err) => {
    alert('请求错误')
    nprogress.done()
    throw err

});

Vue.prototype.$axios_1 = axios_1;
Vue.prototype.$axios_2 = axios_2;

export {
    axios_1,
    axios_2
}
